<?php

class st_registry {
    const REGISTRY_SERIALIZABLE = true;
    const REGISTRY_NORMAL = false;
    protected static $_instance = null;
    protected $_registry = array();
    protected $_config = array(
        'encode' => false,
        'expired' => 30,
        'limiter' => 'private',
        'savePath' => 'default',
    );
    protected $_memcached = null;

    function __construct() {
        $this->_settings();
    }

    protected function _settings() {
        if (session_id() === '') {
            session_start();
        }
        if (count($this->_config) === 0) {
            return $this;
        }
        if (isset($this->_config['expired'])) {
            session_cache_expire($this->_config['expired']);
        }
        if (isset($this->_config['limiter'])) {
            session_cache_limiter($this->_config['limiter']);
        }
        if (isset($this->_config['savePath'])) {
            if ($this->_config['savePath'] != 'default') {
                session_save_path($this->_config['savePath']);
            }
        }
        return $this;
    }

    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self;
        }
        return self::$_instance;
    }

    public function register($key, $value, $serialize = false) {
        if (empty($key)) {
            return false;
        }
        if ($serialize) {
            $value = serialize($value);
        }
        $this->_registry[$key] = $value;
        $_SESSION[$key]['data'] = $value;
        $_SESSION[$key]['serialize'] = (int) $serialize;
        return $this;
    }

    public function registry($key) {
        if (!is_string($key)) {
            throw new Exception("Key Of Registry Object Must Be A String");
        }
        $value = null;
        if (isset($_SESSION[$key]['data'])) {
            if ((int) $_SESSION[$key]['serialize'] === 1) {
                $value = unserialize($_SESSION[$key]['data']);
            } else {
                $value = (string) $_SESSION[$key]['data'];
            }
        }
        return $value;
    }
    
    public function hasRegistry($key) {
        return isset($_SESSION[$key]);
    }

    public function clearRegistry($key = null) {
        if (!$key) {
            $_SESSION = array();
            return $this;
        }
        $_SESSION[$key] = null;
        unset($_SESSION[$key]);
        return $this;
    }

    public function getFlashMessage($key) {
        $message = $this->registry($key);
        unset($_SESSION[$key]);
        return $message;
    }
    
    public function enabledMemcache() {
        if (class_exists('Memcache')) {
            return true;
        }
        return false;
    }
    
    private function __initMemcache() {
        if (!GLOBAL_ENABLED_CACHE || !class_exists('Memcache')) {
            return false;
        }
        if ($this->_memcached === null) {
            $this->_memcached = new Memcache;
        }
        $this->_memcached->connect(MEMCACHE_HOST, MEMCACHE_PORT);
        return $this;
    }
    
    public function setKey($key, $value, $timeToLive, $serialize = true) {
        $is_success = $this->__initMemcache();
        if (!$is_success) {
            return false;
        }
        if ($serialize) {
            $value = serialize($value);
        }
        $key = GLOBAL_PREFIX_CACHE . $key;
        @$this->_memcached->set($key, $value, MEMCACHE_COMPRESSED, $timeToLive);
        return $this;
    }
    
    public function getKey($key, $unserialize = true) {
        $is_success = $this->__initMemcache();
        if (!$is_success) {
            return false;
        }
        $key = GLOBAL_PREFIX_CACHE . $key;
        @$value = $this->_memcached->get($key);
        if (!$value) {
            $value = null;
        }
        if ($unserialize) {
            $value = unserialize($value);
        }
        return $value;
    }
    
    public function deleteKey($key) {
        $is_success = $this->__initMemcache();
        $key = GLOBAL_PREFIX_CACHE . $key;
        if (!$is_success) {
            return false;
        }
        $this->_memcached->delete($key);
        return $this;
    }

    public function clearMemcache() {
        $is_success = $this->__initMemcache();
        if (!$is_success) {
            return false;
        }
        $this->_memcached->flush();
        return $this;
    }
}

?>